package com.ie.service.before;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ie.common.http.ResponseResult;
import com.ie.common.security.utils.ConfigrarionBean;
import com.ie.common.security.utils.JwtTokenUtil;
import com.ie.common.security.utils.MD5Util;
import com.ie.common.security.utils.RedisUtil;
import com.ie.entity.BUserEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ie.mapper.before.BUserMapper;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class BUserServiceImpl extends ServiceImpl<BUserMapper, BUserEntity> implements BUserService {
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private JwtTokenUtil jwtTokenUtil;
    @Autowired
    private ConfigrarionBean config;

    @Override
    public ResponseResult<Map<String, String>> register(BUserEntity bUserEntity) {
        bUserEntity.setBpwd(MD5Util.MD5(bUserEntity.getBpwd()));
        Long count = lambdaQuery().eq(BUserEntity::getBemail, bUserEntity.getBemail()).count();
        if(count > 0){
            return ResponseResult.getMessageResult(null, "A001");
        }
        else if (save(bUserEntity)){
            return ResponseResult.getMessageResult(null, "A002");
        }
        else{
            return ResponseResult.getMessageResult(null, "A003");
        }
    }

    @Override
    public ResponseResult<Map<String, String>> login(BUserEntity bUserEntity) {
        // 从redis中获取验证码
        String code = (String)redisUtil.get("code");
        // 如果验证码不匹配，则返回null
        if(!code.equalsIgnoreCase(bUserEntity.getCode())){
            return ResponseResult.getMessageResult(null, "A000");
        }else{
            System.out.println(code);
        }
        Long count = lambdaQuery().eq(BUserEntity::getBemail, bUserEntity.getBemail()).count();
        if(count == 0){
            return ResponseResult.getMessageResult(null, "A001");
        }
        bUserEntity.setBpwd(MD5Util.MD5(bUserEntity.getBpwd()));
        List<BUserEntity> list = lambdaQuery()
                .eq(BUserEntity::getBemail, bUserEntity.getBemail())
                .eq(BUserEntity::getBpwd,  bUserEntity.getBpwd()).list();
        if(list.size() > 0){

            bUserEntity=list.get(0);
            System.out.println(bUserEntity);

            String token = jwtTokenUtil.createToken((bUserEntity.getId()).toString());
//            redisUtil.set("login_" + bUserEntity.getBemail(), bUserEntity.getBemail(), config.getExpiration());
            Map<String, String> myres = new HashMap<>();
            myres.put("token", token);
            myres.put("email", bUserEntity.getBemail());
            myres.put("bid", list.get(0).getId() + "");
            return ResponseResult.getSuccessResult(myres);
        }
        else{
            return ResponseResult.getMessageResult(null, "A002");
        }
    }
}
